/**
 * FileName: LRUCache
 * Author:   powersi
 * Date:     2021/3/8 15:22
 * Description:
 */
package com.zm.demo.core.collection;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Description: 
 * Author:      powersi
 * Date:        2021/3/8 15:22
 */
public class LRUCache<K,V> extends LinkedHashMap<K,V> {

    private long cacheSize;
    public LRUCache(long cacheSize){
        super((int) (Math.ceil(cacheSize/0.75)+1), 0.75f, true);
        this.cacheSize = cacheSize;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > this.cacheSize;
    }


    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(3);

        lruCache.put(1,1);
        lruCache.put(2,1);
        lruCache.put(3,1);
        lruCache.put(4,1);
        lruCache.put(5,1);
        lruCache.get(3);
        lruCache.put(6,1);
//        lruCache.put(7,1);
        System.out.println(lruCache.toString());
    }
}
